//
//  HWTrumpetViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2021/7/15.
//  Copyright © 2021 Chendaobin. All rights reserved.
//

#import "HWTrumpetViewController.h"
#import "HWRecyclingTableViewCell.h"
#import "HWhuishouModel.h"
#import "HWRecyclingRecordsViewController.h"
@interface HWTrumpetViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(strong,nonatomic) UITableView *mainaTableVie;
@property(strong,nonatomic) UIView *headerView;
@property(strong,nonatomic) UIView *fengexiangView;
@property(strong,nonatomic) NSString *panduanStr;
@property(strong,nonatomic) UILabel *huishouxuzhiLabel;
@property(strong,nonatomic) UILabel *xuzhitishiLabel;
@property(strong,nonatomic) UILabel *lingyigmiaoshulabel;
@property(strong,nonatomic) NSDictionary *configDic;
@property(strong,nonatomic) UIView *lowerPartView;
@property(strong,nonatomic) NSMutableArray *dataMuArr;
@property(strong,nonatomic) HWRecyclingTableViewCell *selecell;
@property(strong,nonatomic)UIButton *tijiaoBut;






@end

@implementation HWTrumpetViewController

-(UITableView *)mainaTableVie {

    if (_mainaTableVie == nil
        ) {


        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-44-CF_TabbarSafeBottomMargin) style:UITableViewStylePlain];
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainaTableVie.backgroundColor =RGB(247, 247, 247);
    


    }
    return   _mainaTableVie ;
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self sendingANetworkRequest];
    self.navigationController.navigationBarHidden = NO;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupnaitionView];
    [self setupUI];
    [self addheadeView];
    [self addLowerPartView];
  //   [self sendingANetworkRequest];
    
   
}
// 设置导航栏
- (void)setupnaitionView {
    
    self.panduanStr   = @"2";
    
    self.navigationItem.title = @"小号回收";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"回收记录" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarTap)];
    self.view.backgroundColor = RGB(247, 247, 247);
    
    
    
}
- (void)addLowerPartView {
    
    self.lowerPartView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenH-44-CF_TabbarSafeBottomMargin, kScreenW, 44+CF_TabbarSafeBottomMargin)];
    // self.lowerPartView.backgroundColor = [UIColor yellowColor];
    self.lowerPartView.backgroundColor = RGB(247, 247, 247);
    [self.view addSubview:self.lowerPartView];
    
    UIButton *butt  = [UIButton setButtonWithFrame:CGRectMake(15, 0, kScreenW-15*2, 34) Title:@"提交" Image:nil Font:13 Color:RGB(255, 255, 155) Target:self Selector:@selector(buttonTap:)];
   //   self.downloadBut = butt;
     butt.backgroundColor = RGB(38, 154, 255);
     butt.layer.cornerRadius = 15;
     butt.layer.masksToBounds = YES;
     butt.backgroundColor = [UIColor bm_colorGradientChangeWithSizeView: butt kaish:RGB(146, 180, 255) endjishu:RGB(79, 134, 255)];
    self.tijiaoBut = butt;
    self.tijiaoBut.hidden = YES;
     [self.lowerPartView addSubview:butt];
    
}



// 点击回收记录
- (void)rightBarTap {
    
    
    HWRecyclingRecordsViewController *recycling = [[HWRecyclingRecordsViewController alloc]init];
    [self.navigationController pushViewController:recycling animated:YES];
    
    
}

//点击提交按钮
- (void)buttonTap:(UIButton *)sender  {
    
    
    if (self.selecell == nil) {
        
        [MBProgressHUD showError:@"请选择小号"];
        return;
        
        
    }
    
    NSIndexPath *index =   [self.mainaTableVie indexPathForCell:self.selecell];
    
    HWhuishouModel   *model     =   self.dataMuArr[index.row];
    
    
    NSString  *small_uid = model.small_uid;
    
    NSString *userID = [HWLocallyStoredData getLoginUserID];
    NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,small_uid,@"fsd213ewdsadqwe2121213edsad"];
    NSString *signMD5 = [WMUtil md5HexDigest:sign];
    NSDictionary *parametes = @{
                                @"ct" :  @"app",
                                @"ac" : @"recover_add",
                                @"ios" : @"1",
                                @"uid" :userID,
                                @"small_uid":small_uid,
                                @"sign": signMD5
                                };
    
    
   //  [MBProgressHUD show:@"正在刷新"];
    
    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
        
      
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
     //   [MBProgressHUD hideHUD];
        
        
        if ([codeStr intValue] == 1) {
         
          //  self.fenleiMuArr = [response[@"data"] mutableCopy];
          //   [MBProgressHUD  showError:@"加载成功"];
            [self.dataMuArr removeObjectAtIndex:index.row];
            
            if (self.dataMuArr.count > 0 ) {
                self.panduanStr = @"1";
              
                
                
                
            } else {
                
            
                self.panduanStr = @"2";
               
                
            }
            
            [self sendingANetworkRequest];
            
    
            [self.mainaTableVie reloadData];
            
            
         
            

        } else {
        
            
        }
        NSLog(@"%@",response);
        
        
    } failure:^(NSString *error) {
      
        [MBProgressHUD  showError:@"加载失败"];
        
    }];
    
    
    
    
    
    
    
}

// 设置UI
- (void)setupUI  {
    
    
    self.mainaTableVie.delegate = self;
    self.mainaTableVie.dataSource = self;
    [self.view addSubview:self.mainaTableVie];
    
    
    
    
}

// 添加headerView
- (void)addheadeView {
    
    self.headerView =  [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 280)];
    self.headerView.backgroundColor = RGB(247, 247, 247);
    self.mainaTableVie.tableHeaderView =  self.headerView;

    UIView *baiseView = [[UIView alloc]initWithFrame:CGRectMake(15, 10, kScreenW-30, 270)];
    baiseView.backgroundColor = [UIColor whiteColor];
    baiseView.layer.cornerRadius = 12;
    baiseView.layer.masksToBounds = YES;
    [self.headerView addSubview:baiseView];
    
    CGFloat   jianju  =  (kScreenW-30 -30-64*3)/2;
    for (int i = 0 ; i < 3; i++) {
    
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 18, 64, 64)];
        label.backgroundColor = RGB(255, 217, 161);
        label.layer.cornerRadius = 32;
        label.layer.masksToBounds = YES;
        label.textColor = RGB(68, 68, 68);
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:12];
        [baiseView addSubview:label];
        
        if (i == 0) {
            label.frame = CGRectMake(15, 18, 64, 64);
          //   label.backgroundColor = RGB(255, 217, 161);
            label.backgroundColor = RGB(255, 217, 161);
            
            label.text = @"选择小号";
            
            
        } else if (i == 1) {
            
            label.frame = CGRectMake(15+i*(64+jianju), 18, 64, 64);
           //  label.backgroundColor = RGB(255, 151, 150);
            label.backgroundColor = RGB(251, 195, 68);
            label.text = @"确认提交";
            
        } else {
            
            label.frame = CGRectMake(15+i*(64+jianju), 18, 64, 64);
            label.backgroundColor = RGB(146, 180, 255);

            label.text = @"实时到账";
            
        }
        
    }
    
    CGFloat   liabian =  (jianju -43)/2;
    
    UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(15+64+liabian, 18+27, 43, 11)];
    imageView1.image = [UIImage imageNamed:@"zhixiangjiantou"];
    imageView1.image = [UIImage imageNamed:@"xingyaobangyoubianjiantou"];
    [baiseView addSubview:imageView1];
    
    UIImageView *imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(15+jianju+64*2+liabian, 18+27, 43, 11)];
    imageView2.image = [UIImage imageNamed:@"xingyaobangyoubianjiantou"];
    [baiseView addSubview:imageView2];
    
    self.fengexiangView = [[UIView alloc]initWithFrame:CGRectMake(8, 64+18+10, kScreenW-16-30, 0.5)];
    self.fengexiangView.backgroundColor = RGB(236, 236, 236);
   // self.fengexiangView.backgroundColor = [UIColor redColor];
    [baiseView addSubview:self.fengexiangView];
    
    
    self.huishouxuzhiLabel  = [[UILabel alloc]initWithFrame:CGRectMake(14, self.fengexiangView.bottom+10,100, 17)];
   //  self.huishouxuzhiLabel.backgroundColor = RGB(255, 217, 161);
//    self.huishouxuzhiLabel.layer.cornerRadius = 32;
//    self.huishouxuzhiLabel.layer.masksToBounds = YES;
    self.huishouxuzhiLabel.textColor = RGB(51 , 51, 51);
    self.huishouxuzhiLabel.textAlignment = NSTextAlignmentLeft;
    self.huishouxuzhiLabel.font = [UIFont systemFontOfSize:12];
    self.huishouxuzhiLabel.text = @"回收须知：";
    [baiseView addSubview:self.huishouxuzhiLabel];
    
    
    self.xuzhitishiLabel  = [[UILabel alloc]initWithFrame:CGRectMake(14, self.huishouxuzhiLabel.bottom+5,kScreenW-30 -28, 80)];
   //  self.huishouxuzhiLabel.backgroundColor = RGB(255, 217, 161);
//    self.huishouxuzhiLabel.layer.cornerRadius = 32;
//    self.huishouxuzhiLabel.layer.masksToBounds = YES;
    self.xuzhitishiLabel.textColor = RGB(102 , 102, 102);
    self.xuzhitishiLabel.textAlignment = NSTextAlignmentLeft;
    self.xuzhitishiLabel.font = [UIFont systemFontOfSize:12];
  //  self.xuzhitishiLabel.text = @"1.只针对具有实际充值记录的小号（不包含优惠部分）进行回收，且回收方式为平台币回收，部分较老或已经关服的游戏不支持回收。 \n 2.用户在提交符合要求的小号后，按照实际充值金额的5%回收，回收成功后24小时内可赎回，逾期不可赎回。";
    self.xuzhitishiLabel.numberOfLines = 0;
    [baiseView addSubview:self.xuzhitishiLabel];
    
    
    self.lingyigmiaoshulabel  = [[UILabel alloc]initWithFrame:CGRectMake(14,   self.xuzhitishiLabel.bottom,kScreenW-30 -28, 55)];
   //  self.huishouxuzhiLabel.backgroundColor = RGB(255, 217, 161);
//    self.huishouxuzhiLabel.layer.cornerRadius = 32;
//    self.huishouxuzhiLabel.layer.masksToBounds = YES;
    self.lingyigmiaoshulabel.textColor = RGB(249 , 58, 55);
    self.lingyigmiaoshulabel.textAlignment = NSTextAlignmentLeft;
    self.lingyigmiaoshulabel.font = [UIFont systemFontOfSize:12];
   // self.lingyigmiaoshulabel.text = @"PS：目前赎回仅支持现金赎回；确认提交后，请耐心等待工作人员审核，请勿再登录小号或对小号进行充钱，如因未按规则执行造成的损失平台概不负责";
    self.lingyigmiaoshulabel.numberOfLines = 0;
    [baiseView addSubview:self.lingyigmiaoshulabel];
    
    
    
    
    
    
    
    

    
    
}


// 发送网路请求
- (void)sendingANetworkRequest {
    
    
    NSString *userID = [HWLocallyStoredData getLoginUserID];
    NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
    NSString *signMD5 = [WMUtil md5HexDigest:sign];
    NSDictionary *parametes = @{
                                @"ct" :  @"app",
                                @"ac" : @"recover",
                                @"ios" : @"1",
                                @"uid" :userID,
                                @"t" :tStr,
                                @"sign": signMD5
                                };
    
    
   //  [MBProgressHUD show:@"正在刷新"];
    
    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
        
      
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
     //   [MBProgressHUD hideHUD];
        
        
        if ([codeStr intValue] == 1) {
         
          //  self.fenleiMuArr = [response[@"data"] mutableCopy];
          //   [MBProgressHUD  showError:@"加载成功"];
        
            self.configDic = response[@"config"];
            
    
          //    [self refreshtheInterfaceView];
            
            
          //  self.panduanStr = @"1";
            
            self.dataMuArr = [HWhuishouModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
            
            if (self.dataMuArr.count >0 ) {
                self.panduanStr = @"1";
                self.tijiaoBut.hidden = NO;
            } else {
                
                
                self.panduanStr = @"2";
                self.tijiaoBut.hidden = YES;
            }
            
            [self refreshTheInterfaceView];
            

        } else {
            
             // [MBProgressHUD  showError:@"加载失败"];
            self.dataMuArr = [[NSMutableArray alloc]init];
            
             self.panduanStr = @"2";
            self.configDic = response[@"config"];
            [self refreshTheInterfaceView];
             
            
        }
        NSLog(@"%@",response);
        
        
    } failure:^(NSString *error) {
      
        self.dataMuArr = [[NSMutableArray alloc]init];
        [MBProgressHUD  showError:@"加载失败"];
        
    }];
    
    
    
}

// 刷新界面
- (void)refreshTheInterfaceView {
    
    NSString *diyigeStr = [NSString stringWithFormat:@"%@",self.configDic[@"content"]];
    NSString *twoStr =  [NSString stringWithFormat:@"PS：%@",self.configDic[@"content2"]];
    
    self.xuzhitishiLabel.text = diyigeStr;
    self.lingyigmiaoshulabel.text = twoStr;
    
    [self.mainaTableVie reloadData];
    
  
    
    
}








#pragma mark ------------ UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    
    if([self.panduanStr  isEqualToString:@"2"]) {
        
        return  1;
    }
    
    
    return  self.dataMuArr.count ;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  {
    
    
    if([self.panduanStr  isEqualToString:@"2"]) {
        
        return  190;
    }
    
    
    return  89;
    
    
}





- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    if ([self.panduanStr isEqualToString:@"2"]) {
        
        static NSString  *cellID = @"cellID";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = RGB(247, 247, 247);
        
        UIView *baiseView = [[UIView alloc]initWithFrame:CGRectMake(15, 0, kScreenW-30, 190)];
        baiseView.layer.cornerRadius = 10;
        baiseView.layer.masksToBounds = YES;
        baiseView.backgroundColor = RGB(255, 255, 255);
        [cell.contentView addSubview:baiseView];
        
        UIImageView *icoiamge = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenW-30-82)/2, 70, 82, 44)];
        icoiamge.image = [UIImage imageNamed:@"zanwuxihao"];
      //  icoiamge.backgroundColor = [UIColor redColor];
        [baiseView addSubview:icoiamge];
        
        
        
        
        UILabel *wuLabel  = [[UILabel alloc]initWithFrame:CGRectMake(0,icoiamge.bottom+10,kScreenW-30, 17)];
       //  self.huishouxuzhiLabel.backgroundColor = RGB(255, 217, 161);
    //    self.huishouxuzhiLabel.layer.cornerRadius = 32;
    //    self.huishouxuzhiLabel.layer.masksToBounds = YES;
        wuLabel.textColor = RGB(153 , 153, 153);
        wuLabel.textAlignment = NSTextAlignmentCenter;
        wuLabel.font = [UIFont systemFontOfSize:12];
      //  wuLabel.backgroundColor = [UIColor redColor];
        wuLabel.text = @"暂无小号记录";
        [baiseView addSubview:wuLabel];
        
        
        
        
     
        
        
        return  cell;
        
        
        
    } else {
        
        
        static NSString  *cellID = @"cellID1";
        
        HWRecyclingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell =  [[NSBundle mainBundle]loadNibNamed:@"HWRecyclingTableViewCell" owner:nil options:nil].firstObject;
            
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        HWhuishouModel   *model =  self.dataMuArr[indexPath.row];
        cell.model = model;
        
        UITapGestureRecognizer *tap1 =  [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(xuanzhongTap:)];
        [cell.beijingView addGestureRecognizer:tap1];


        
        
        return  cell;
        
        
    }
    
    



}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    
    
    return  36;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *view =  [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 36)];
    view.backgroundColor = RGB(247 , 247 , 247);
    
    
    UILabel *wuLabel  = [[UILabel alloc]initWithFrame:CGRectMake(15,0,kScreenW-60, 38)];
   //  self.huishouxuzhiLabel.backgroundColor = RGB(255, 217, 161);
//    self.huishouxuzhiLabel.layer.cornerRadius = 32;
//    self.huishouxuzhiLabel.layer.masksToBounds = YES;
    wuLabel.textColor = RGB(68 , 68, 68);
    wuLabel.textAlignment = NSTextAlignmentLeft;
    wuLabel.font = [UIFont systemFontOfSize:16];
  //  wuLabel.backgroundColor = [UIColor redColor];
    wuLabel.text = @"可回收小号";
    [view addSubview:wuLabel];
    
    
    
    return  view;
    
}



#pragma mark -------- 添加点击事件

- (void)xuanzhongTap:(UITapGestureRecognizer *)serder {
    
    HWRecyclingTableViewCell *cell = (HWRecyclingTableViewCell *)[[serder.view superview] superview];
    
    NSLog(@"%@", [[serder.view superview] superview]);
    NSLog(@"%@", [[serder.view superview] superview]);
    self.selecell.beijingView.layer.borderWidth =1;
   //  self.selecell.beijingView.layer.borderColor = RGB(255, 123, 102).CGColor;
    self.selecell.beijingView.layer.borderColor = RGB(255, 255, 255).CGColor;
 
    
    self.selecell = cell;
    
    self.selecell.beijingView.layer.borderWidth =1;
   //  self.selecell.beijingView.layer.borderColor = RGB(255, 123, 102).CGColor;
    self.selecell.beijingView.layer.borderColor = RGB(255, 123, 102).CGColor;
 
    
    
    
    
    
}







@end
